package com.nuanshui.framework.res.qiniu.impl;

import com.nuanshui.framework.res.qiniu.QiniuBean;
import com.nuanshui.framework.utils.redis.JedisUtil;
import com.nuanshui.heatedloan.enums.FileBelong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zxh on 2017/7/2.
 */
@Component
public class QiniuBeanManager {


    Map<String, QiniuBean> mapQiniuBean = null;

    @Value("${qiniu.product}")
    String qiniuProduct;

    @Value("${qiniu.video}")
    String qiniuVideo;


    private void addQiniuBean(String qiniuConfig, String bucket) {
        String[] products = qiniuConfig.split("\\|");
        QiniuBean bean = new QiniuBean(bucket, products[0], products[1],
                products[2], products[3]);
        mapQiniuBean.put(products[0], bean);
    }

    protected synchronized void initMapQiniuBean() {
        if (mapQiniuBean != null)
            return;

        mapQiniuBean = new HashMap<String, QiniuBean>();

        addQiniuBean(qiniuProduct, "product");
        addQiniuBean(qiniuVideo, "video");
    }

    public QiniuBean findQiniuBeanByBucketName(String bucketName) {
        initMapQiniuBean();
        return mapQiniuBean.get(bucketName);
    }

    public QiniuBean findQiniuBeanByBelong(FileBelong belong) {
        initMapQiniuBean();


        QiniuBean currentBean = null;

        String bucketName = null;
        String bucketType = null;

        switch (belong) {
            case PRODUCT:
                bucketType = "product";
                break;
            case VIDEO:
                bucketType = "video";
                break;
            default:
                break;

        }
        for (String key : mapQiniuBean.keySet()) {
            QiniuBean bean = mapQiniuBean.get(key);
            if (bean.bucketType.equals(bucketType)) {
                currentBean = bean;
                break;
            }
        }

        return currentBean;
    }
}
